package com.softer.chengxin.blacklist.dao.provider;

import com.softer.chengxin.common.domin.Page;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.HashMap;
/**
 * @author wangixnrui
 * @date 2018/10/12
 */
public class BlacklistProvider {

    /**
     * 按条件查找该账号下的企业黑名单
     */
    public String findBlacklistByCondition(HashMap<String,Object> map){
        StringBuffer sql = new StringBuffer(20);
        String qyName = (String) map.get("qyName");
        String cxDateBegin = (String) map.get("cxDateBegin");
        String cxDateEnd = (String) map.get("cxDateEnd");
        Integer state = (Integer)    map.get("state");
        Page page = (Page) map.get("page");
        String area = (String) map.get("area");

        sql.append("select c.`name` as qyName,b.id as id,DATE_FORMAT(b.gmt_create, '%Y-%m-%d') as gmtCreate,DATE_FORMAT(b.gmt_modified, '%Y-%m-%d') as gmtModified,b.company_id as companyId,b.score_type_id AS scoreTypeId,(SELECT score_name FROM score_type WHERE id = b.score_type_id) AS scoreTypeName,m.name as adder,b.state as state,b.remark as remark,b.file_id as fileId");
        sql.append(" from company_blacklist b left join company_base_info c on b.company_id = c.id left join manager_user m on m.login_name = b.adder");
        sql.append(" where c.state in (1,2,3)");

        commonSql(sql,area,qyName,cxDateBegin,cxDateEnd,state);
        sql.append(" LIMIT ");
        sql.append(page.getBeginIndex()).append(",").append(page.getLimit());
        return sql.toString();
    }

    /**
     * 按条件查找该账号下的企业黑名单条数
     */
    public String findBlacklistByConditionCount(HashMap<String,Object> map){
        StringBuffer sql = new StringBuffer(20);
        String qyName = (String) map.get("qyName");
        String cxDateBegin = (String) map.get("cxDateBegin");
        String cxDateEnd = (String) map.get("cxDateEnd");
        Integer state = (Integer) map.get("state");
        String area = (String) map.get("area");

        sql.append("select count(*) from company_blacklist b,company_base_info c");
        sql.append(" where c.state in (1,2,3)");
        sql.append(" and b.company_id = c.id");

        commonSql(sql,area,qyName,cxDateBegin,cxDateEnd,state);

        return sql.toString();
    }

    public void commonSql(StringBuffer sql,String area ,String qyName,String cxDateBegin,String cxDateEnd,Integer state){
        if(StringUtils.isNotBlank(area)) {
            sql.append(" and c.community in (").append(area).append(")");
        }
        if(StringUtils.isNotBlank(qyName)) {
            sql.append(" and c.`name` like CONCAT('%',#{qyName},'%')");
        }
        if(StringUtils.isNotBlank(cxDateBegin)) {
            sql.append(" and b.gmt_create>=#{cxDateBegin}");
        }
        if(StringUtils.isNotBlank(cxDateEnd)) {
            sql.append(" and b.gmt_create<=#{cxDateEnd}");
        }
        if(state!=null) {
            sql.append(" and b.state=#{state}");
        }
    }

}
